package com.endurance.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: sunlin
 * @Date: 2024-04-26 15:48
 * @Version: 1.0
 * @Description: 测试Controller
 */

@RestController
public class SentinelController {

    @Value("${server.port}")
    private String port;

    private int num;

    @GetMapping("/sentinel")
    // 给资源起别名，最好使用这种方式设置资源名称，解耦合
    @SentinelResource(value = "sentinelResources")
    public String sentinel() {
        int i = 1 / 0;
        num++;
        System.out.println("sentinel:" + num);

        return "sentinel:" + port + ":" + num;
    }

    @GetMapping("/sentinel1")
    // 给资源起别名，最好使用这种方式设置资源名称，解耦合
    @SentinelResource(value = "sentinelResources1", fallback = "sentinelFallback")
    public String sentinel1() throws InterruptedException {
        num++;
        System.out.println("sentinel1:" + num);
        return "sentinel1:" + port + ":" + num;
    }

    /**
     * 热点参数规则
     *
     * @return
     */
    @GetMapping("/sentinel2")
    // 给资源起别名，最好使用这种方式设置资源名称，解耦合
    @SentinelResource(value = "sentinelResources3")
    public String sentinel2(String param1, String param2) {

        return param1 + ":" + param2;
    }

    public String sentinelFallback() {
        return "sentinelFallback";
    }


}
